﻿using sys = System;
using sysconsole = System.Console;
using color = System.ConsoleColor;
using SplitOS.OSAL.INTERNAL;
using SplitOS.OSAL.Math;

namespace SplitOS.OSAL
{
    public static class Interpreter
    {
        public static bool isStopped = false;
        public static void Start()
        {
            isStopped = false;
            sysconsole.Clear();
            sysconsole.Write("---------------[ ");
            Out.WriteTitle("Open SplitOS Application Language", color.Blue);
            sysconsole.WriteLine(" ]---------------");
        mainloop:
            if (!isStopped)
            {
                sysconsole.Write("$ ");
                Parse(sysconsole.ReadLine());
                goto mainloop;
            }
        }
        public static void Stop() { isStopped = true;  sysconsole.Clear(); Core.PrintLogo(); }

        public static bool Binary = false;
        public static string META_author, META_title;
        public static string CODE;
        public static void Parse(object ToParse)
        {
            if (ToParse is int)
            {
                // Start Math Module
            }
            else
            {
                string cmd = ((string)ToParse).Trim();
                string lcmd = ((string)ToParse).Trim().ToLower();

                if (cmd == "BINARY START") { Binary = true; CODE += "BINARY"; }
                else if (StringTools.StartsWith(cmd, "META TITLE ") && Binary)
                {
                    META_title = cmd.Split("META TITLE ".ToCharArray())[1];
                    Out.WriteLine("META-Information set: Title    Value: " + META_title);
                    CODE += "\n" + cmd;
                }
                else if (StringTools.StartsWith(cmd, "META AUTHOR ") && Binary)
                {
                    META_author = cmd.Split("META AUTHOR ".ToCharArray())[1];
                    Out.WriteLine("META-Information set: Author   Value: " + META_author);
                    CODE += "\n" + cmd;
                }
                else if (StringTools.StartsWith(cmd, "PRINT ") && Binary) CODE += "\n" + cmd;
                else if (StringTools.StartsWith(cmd, "PRINTLN ") && Binary) CODE += "\n" + cmd;
                else if (StringTools.StartsWith(cmd, "//") && Binary) { }
                else if (cmd == "BINARY END")
                {
                    Binary = false;
                    Out.Write("Compiling your Application...");
                    Storage.AddApplication(META_title, CODE);
                    Out.WriteLine(" Done", sys.ConsoleColor.Green);
                    Out.WriteLine("Run your application by typing 'SAL RUN TITLE'");
                }
            }
        }
    }
}